package com.icerhe.bmi;

import java.text.DecimalFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Bmi extends Activity {
	
	private Button calcbutton;
	private EditText fieldheight;
	private EditText fieldweight;
    private TextView view_result;
    private TextView view_suggest;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //listen for button click
        findViews();
        setListeners();
    }
    
    private void findViews()
    {
        calcbutton = (Button) findViewById(R.id.submit);
        fieldheight = (EditText) findViewById(R.id.height);
        fieldweight = (EditText) findViewById(R.id.weight);
        view_result = (TextView) findViewById(R.id.result);
        view_suggest = (TextView) findViewById(R.id.suggest);
    }
    
    //Listen for button clicks
    private void setListeners() {
        calcbutton.setOnClickListener(calcBMI);
    }
    
    private Button.OnClickListener calcBMI = new Button.OnClickListener()
    {
        public void onClick(View v)
        {
            DecimalFormat nf = new DecimalFormat("0.0");
            double height = Double.parseDouble(fieldheight.getText().toString())/100;
            double weight = Double.parseDouble(fieldweight.getText().toString());
            double BMI = weight / (height * height);
            
            //Present result 
            view_result.setText(getText(R.string.bmi_result) + nf.format(BMI));
 
            //Give health advice 
            if(BMI>25){
                view_suggest.setText(R.string.heavy);
            }else if(BMI<20){
                view_suggest.setText(R.string.light);
            }else{
                view_suggest.setText(R.string.average);
            }
            
            openOptionsDialog();
        }
    };
    
    private void openOptionsDialog() {
    	Toast.makeText(Bmi.this, "BMI 計算器", Toast.LENGTH_SHORT).show();
        
    	new AlertDialog.Builder(Bmi.this)
        .setTitle(R.string.about_title)
        .setMessage(R.string.about_msg)
        .setPositiveButton(R.string.ok_label,
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialoginterface, int i) {
								// Do nothing
							}
						})
		.setNegativeButton(R.string.homepage_label,
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialoginterface, int i) {
								// go to url
								Uri uri = Uri.parse("http://androidbmi.googlecode.com/");
								Intent intent = new Intent(Intent.ACTION_VIEW,	uri);
								startActivity(intent);
							}
						})
        .show();
        
    }
    
}